<?php
/*
 * Created  : Mon 16 Nov 2009 07:48:57 PM EST
 * Modified : Tue 04 Jan 2011 11:36:30 PM EST
 *
 * Copyright 2009 Gautam Iyer <gi1242@nospam.com> (Replace nospam with gmail).
 *
 * This file is part of phpss.
 *
 * phpss is free software: you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later 
 * version.
 *
 * phpss is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 * details.
 *
 * You should have received a copy of the GNU General Public License along with 
 * phpss.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

fwrite( STDERR, "Writing index.html\n" );

is_dir( $output_dir ) or mkdir( $output_dir, 0777, true)
  or die( "Could not create $output_dir\n" );
$outfile = fopen( "$output_dir/index.html", "w" )
  or die( "Could not open $output_dir/index.html for writing\n" );

fwrite( $outfile, <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" type="text/css" href="${shared_prefix}black.css" />
    <script type="text/javascript" language="javascript"
      src="${shared_prefix}lytebox.js"></script>
    <link rel="stylesheet" type="text/css" media="screen"
      href="${shared_prefix}lytebox.css" />
    <title>$title</title>
  </head>

  <body>
    <div class="Centered">
      <h1>$title</h1>
      <h4>$subtitle</h4>

      $header
      <div class="ImageList">\n
EOF
  ) or die( "Could not write to index.html\n" );

foreach( $files as $f )
{
  /*
   * First try and figure out what the caption should be.
   */
  $cap = NULL;
  $bn_f = basename( $f );

  if(
      isset( $captions ) && is_array( $captions ) &&
	array_key_exists( $f, $captions )
    )
    $cap = $captions[$f];
  else
    $cap = iptc_caption( $f );
  $cap = htmlentities( $cap );

  fwrite( $outfile, <<<EOF
	<a href="${image_dir}/$bn_f" rel="lytebox[phpss]" title="$cap">
	  <img class="Thumbnail" src="${thumb_dir}/$bn_f" alt="$bn_f"
	    title="$cap" />
	</a>\n
EOF
  ) or die( "Could not write to index.html\n" );
} /*foreach( $files as $f );*/

$timestamp = strftime( "%c" );
fwrite( $outfile, <<<EOF
      </div> <!-- ImageList -->
      <br />
      <div class="Signature">
	$contact_info
	<hr class="Signature" />
	$logo
	$quote
	<p class="Timestamp">Last modified: $timestamp</p>
      </div> <!-- Signature -->
    </div> <!-- Centered -->
  </body>
</html>
EOF
  ) or die( "Could not write to index.html\n" );

fclose( $outfile )
  or die("Could not close output\n");

/*
 * Get IPTC caption from file.
 */
function iptc_caption( $f )
{
  try
  {
    $tag = NULL;
    $cap = NULL;

    getimagesize( $f, $info );
    if( !is_array($info) || !array_key_exists( 'APP13', $info )  )
      throw new Exception("No IPTC Info for $f");

    $iptcinfo = iptcparse( $info["APP13"] );
    if( !$iptcinfo ) 
      throw new Exception("Could not parse APP13 IPTC info");

    if(
	array_key_exists( IPTCCaption, $iptcinfo ) &&
	is_array( $iptcinfo[IPTCCaption] ) &&
	$iptcinfo[IPTCCaption][0] != ""
      )
      $cap = $iptcinfo[IPTCCaption][0];

    if(
	array_key_exists( IPTCKeywords, $iptcinfo ) &&
	is_array( $iptcinfo[IPTCKeywords] )
      )
    {
      foreach( $iptcinfo[IPTCKeywords] as $keyw )
      {
	if( isset( $tag ) )
	  $tag .= ", ";
	$tag .= basename( $keyw );
	//preg_replace( '/(^|,\s*)[^,]*\/', '$1', $tag );
      }
    }

    // fwrite( STDERR, "f=$f, cap=$cap, tag=$tag.\n" );

    if( isset( $cap ) && isset( $tag ) ) 
      return "$cap <span style='font-weight: normal; font-style: italic'>" .
	"(Tags: $tag)</span>";
    elseif( isset( $tag ) )
      return "Tags: <span style='font-weight: normal;'>$tag</span>";
    else
      return basename( $f );
  }
  catch( Exception $e )
  {
    fwrite( STDERR, IT . $e->getMessage() . "\n" . RE );
    return basename( $f );
  }
}
?>
